package com.htu.courseService.courseContents.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.htu.courseService.common.util.MinioUtil;
import com.htu.courseService.common.util.UserUtil;
import com.htu.courseService.courseContents.dao.DirectoriesMapper;
import com.htu.courseService.courseContents.dao.FileMapper;
import com.htu.courseService.courseContents.entity.Directories;
import com.htu.courseService.courseContents.entity.File;
import com.htu.courseService.courseContents.service.FileService;
import com.htu.courseService.courseManage.dao.CoursesMapper;
import com.htu.courseService.myCourse.dao.MyCoursesMapper;
import com.htu.courseService.myCourse.entity.UserCourses;
import com.htu.courseService.user.vo.UserInfo;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created By JNNarrator on 2025/1/12 09:28
 * email: jiangn09@chinatelecom.cn
 */
@Service
@RequiredArgsConstructor
public class FileServiceImpl implements FileService {
	private final MinioUtil minioUtil;
	private final FileMapper fileMapper;
	private final CoursesMapper coursesMapper;
	private final MyCoursesMapper myCoursesMapper;
	private final DirectoriesMapper directoriesMapper;
	@Override
	public void uploadFile(MultipartFile file, Integer directoryId) {
		String fileName = file.getOriginalFilename();
		String fileUrl = minioUtil.upload(file);
		String originalFilename = file.getOriginalFilename();
		String fileType = "";
		if (originalFilename != null && originalFilename.contains(".")) {
			fileType =  originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
		}
		File fileEntity = new File(null, directoryId, fileName,fileUrl, fileType, null);
		fileMapper.insert(fileEntity);
	}
	
	@Override
	public List<File> list(Integer directoryId) {
		LambdaQueryWrapper<Directories> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(Directories::getDirectoryId,directoryId);
		Directories directories = directoriesMapper.selectOne(wrapper);
		UserInfo userInfo = UserUtil.getUserInfo();
		LambdaQueryWrapper<UserCourses> wrapperOne = new LambdaQueryWrapper<>();
		wrapperOne.eq(UserCourses::getUserId,userInfo.getId());
		wrapperOne.eq(UserCourses::getCourseId,directories.getCourseId());
		Long count = myCoursesMapper.selectCount(wrapperOne);
		QueryWrapper<File> fileQueryWrapper = new QueryWrapper<>();
		fileQueryWrapper.eq("directory_id",directoryId);
		List<File> fileList = fileMapper.selectList(fileQueryWrapper);
		if(count == 0){
			for(File file : fileList){
				file.setFileUrl(null);
			}
		}
		return fileList;
	}
	
	@Override
	public void downloadFile(HttpServletResponse response, Integer fileId) {
		File file = fileMapper.selectById(fileId);
		minioUtil.download(file.getFileUrl(),response);
	}

	@Override
	public void deleteFile(Integer fileId) {
		File file = fileMapper.selectById(fileId);
		minioUtil.delete(file.getFileUrl());
		fileMapper.deleteById(fileId);
	}
}
